string Indicator_Name = "_CurrencyStrength_";

#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Blue
#property indicator_color4 Red

extern double koef_W1 = 1;
extern double koef_1440 = 0.5;
extern double koef_240 = 0.25;
extern double koef_60 = 0.06;
extern double koef_30 = 0.03;
extern double koef_15 = 0.015;
extern double koef_5 = 0.005;
extern bool show_rect = false;
extern int font_size = 20;
extern color color_Symbol1 = FireBrick;
extern color color_Symbol2 = Olive;
extern color color_UpArrow = Blue;
extern color color_DwArrow = Red;
extern double Alert_level = 0.0;
extern string UpSound = "alert.wav";
extern string DwSound = "alert.wav";

//---- buffers
double Buffer_1[];
double Buffer_2[];
double CrossUp[];
double CrossDown[];

double Buffer_Open[];
double Buffer_Close[];
double Buffer_High[];
double Buffer_Low[];

double EURUSD;
double EURGBP;
double EURCAD;
double EURCHF;
double EURJPY;
double EURAUD;
double EURNZD;
double GBPUSD;
double GBPCAD;
double GBPCHF;
double GBPJPY;
double GBPAUD;
double GBPNZD;
double USDCAD;
double USDCHF;
double USDJPY;
double AUDUSD;
double NZDUSD;
double CADCHF;
double CADJPY;
double AUDCAD;
double NZDCAD;
double AUDCHF;
double AUDJPY;
double AUDNZD;
double NZDCHF;
double NZDJPY;
double CHFJPY;

bool isEURUSD = false;
bool isEURGBP = false;
bool isEURCAD = false;
bool isEURCHF = false;
bool isEURJPY = false;
bool isEURAUD = false;
bool isEURNZD = false;
bool isGBPUSD = false;
bool isGBPCAD = false;
bool isGBPCHF = false;
bool isGBPJPY = false;
bool isGBPAUD = false;
bool isGBPNZD = false;
bool isUSDCAD = false;
bool isUSDCHF = false;
bool isUSDJPY = false;
bool isAUDUSD = false;
bool isNZDUSD = false;
bool isCADCHF = false;
bool isCADJPY = false;
bool isAUDCAD = false;
bool isNZDCAD = false;
bool isAUDCHF = false;
bool isAUDJPY = false;
bool isAUDNZD = false;
bool isNZDCHF = false;
bool isNZDJPY = false;
bool isCHFJPY = false;

bool started = false;
datetime timestart = 0;
string aSymbol1, aSymbol2;
datetime CheckTime;
datetime g_time_20;
string gs_msg;
double alprice;
double point;

datetime lastTime;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   started = false;
   timestart = TimeLocal();
   aSymbol1 = StringSubstr(Symbol(),0,3);
   aSymbol2 = StringSubstr(Symbol(),3,3);
   

//----

   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   rm(aSymbol1);
   rm(aSymbol2);
   ObjectsDeleteAll();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start(){
   if (!started){
      int cur = 0; 
      int st = 30; 
      
      SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,1);       
      SetIndexBuffer(0,Buffer_1); 
      SetIndexLabel(0,aSymbol1 +"/"+ aSymbol2 );
      
      SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,1);       
      SetIndexBuffer(1,Buffer_2); 
      SetIndexLabel(1,aSymbol1 +"/"+ aSymbol2 );
      
      SetIndexBuffer(2, CrossUp);
      SetIndexStyle(2, DRAW_ARROW);
      SetIndexArrow(2, 233);
   
      SetIndexBuffer(3, CrossDown);
      SetIndexStyle(3, DRAW_ARROW);
      SetIndexArrow(3, 234);
      
      SetIndexBuffer(4, Buffer_Open);
      SetIndexLabel(4, "Open"); 
      SetIndexBuffer(5, Buffer_Close);
      SetIndexLabel(5, "Close");
      SetIndexBuffer(6, Buffer_High);
      SetIndexLabel(6, "High");
      SetIndexBuffer(7, Buffer_Low);     
      SetIndexLabel(7, "Low");
      
      if (aSymbol1 =="EUR" || aSymbol2 =="EUR" ){         isEURUSD = true; isEURGBP = true; isEURCAD = true; isEURCHF = true; isEURJPY = true; isEURAUD = true; isEURNZD = true;     }
      if (aSymbol1 =="GBP" || aSymbol2 =="GBP" ){         isGBPUSD = true; isEURGBP = true; isGBPCAD = true; isGBPCHF = true; isGBPJPY = true; isGBPAUD = true; isGBPNZD = true;     }
      if (aSymbol1 =="USD" || aSymbol2 =="USD" ){         isEURUSD = true; isGBPUSD = true; isUSDCAD = true; isUSDCHF = true; isUSDJPY = true; isAUDUSD = true; isNZDUSD = true;     }
      if (aSymbol1 =="CAD" || aSymbol2 =="CAD" ){         isEURCAD = true; isGBPCAD = true; isUSDCAD = true; isCADCHF = true; isCADJPY = true; isAUDCAD = true; isNZDCAD = true;     }
      if (aSymbol1 =="AUD" || aSymbol2 =="AUD" ){         isEURAUD = true; isGBPAUD = true; isAUDCAD = true; isAUDCHF = true; isAUDJPY = true; isAUDUSD = true; isAUDNZD = true;     }
      if (aSymbol1 =="NZD" || aSymbol2 =="NZD" ){         isEURNZD = true; isGBPNZD = true; isNZDCAD = true; isNZDCHF = true; isNZDJPY = true; isNZDUSD = true; isAUDNZD = true;     }
      if (aSymbol1 =="CHF" || aSymbol2 =="CHF" ){         isEURCHF = true; isGBPCHF = true; isCADCHF = true; isUSDCHF = true; isCHFJPY = true; isAUDCHF = true; isNZDCHF = true;     }
      if (aSymbol1 =="JPY" || aSymbol2 =="JPY" ){         isEURJPY = true; isGBPJPY = true; isCADJPY = true; isCHFJPY = true; isUSDJPY = true; isAUDJPY = true; isNZDJPY = true;     }

      started = true;
   }
   
   int limit;
   int    counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
//---- TODO: add your code here
   for (int i=limit; i>=0; i--) { 

   //for(int i=0;i<limit;i++){  
      double nSymbol1 = 0;
      double nSymbol2 = 0;
         LoadData(1440,i);
         nSymbol1+=GetStrength_Symbol(aSymbol1)*koef_W1;
         nSymbol2+=GetStrength_Symbol(aSymbol2)*koef_W1;
         LoadData(240,i);
         nSymbol1+=GetStrength_Symbol(aSymbol1)*koef_1440;
         nSymbol2+=GetStrength_Symbol(aSymbol2)*koef_1440;
         LoadData(60,i);
         nSymbol1+=GetStrength_Symbol(aSymbol1)*koef_240;
         nSymbol2+=GetStrength_Symbol(aSymbol2)*koef_240;
         LoadData(30,i);
         nSymbol1+=GetStrength_Symbol(aSymbol1)*koef_60;
         nSymbol2+=GetStrength_Symbol(aSymbol2)*koef_60;
         LoadData(15,i);
         nSymbol1+=GetStrength_Symbol(aSymbol1)*koef_30;
         nSymbol2+=GetStrength_Symbol(aSymbol2)*koef_30;
         LoadData(5,i);
         nSymbol1+=GetStrength_Symbol(aSymbol1)*koef_15;
         nSymbol2+=GetStrength_Symbol(aSymbol2)*koef_15;
         LoadData(1,i);
         nSymbol1+=GetStrength_Symbol(aSymbol1)*koef_5;
         nSymbol2+=GetStrength_Symbol(aSymbol2)*koef_5;
         Buffer_1[i]=MathMax(0,nSymbol1-nSymbol2);  
         Buffer_2[i]=MathMin(0,nSymbol1-nSymbol2);  
         
         if(Buffer_1[i] > Alert_level && ( Buffer_2[i+1] < 0 || Buffer_1[i+1] < Alert_level ) ) {
         CrossUp[i]   = -1;
         creat_arr("UpCM"+Time[i],Time[i], Open[i],241, color_UpArrow ) ;
         gs_msg = nSymbol1 + "  " + nSymbol2 + "  " + " CM3-v2 Up" ;
         if ( i==0 && CrossUp[i] != EMPTY_VALUE && alprice != CrossUp[i] ) { DoAlerts(UpSound,gs_msg); alprice = CrossUp[i]; }
         }
         if(Buffer_2[i] < -Alert_level && ( Buffer_1[i+1] > 0 || Buffer_2[i+1] > -Alert_level )  ) {
         CrossDown[i] = 1;
         creat_arr("DwCM"+Time[i],Time[i], Open[i],242, color_DwArrow) ;
         gs_msg = nSymbol1 + "  " + nSymbol2 + "  " + " CM3-v2 Dw" ;
         if ( i==0 && CrossDown[i] != EMPTY_VALUE && alprice != CrossDown[i] ) { DoAlerts(DwSound,gs_msg); alprice = CrossDown[i]; }
         }
         
   }
   
   
   if (Buffer_1[0] != 0)
   {
       Buffer_Close[0] = Buffer_1[0];
   }
   else if (Buffer_2[0] != 0)
   {
       Buffer_Close[0] = Buffer_2[0];
   }
   
   if (isNewBar())
   {
       Buffer_Open[0] = Buffer_Close[0];
       Buffer_High[0] = Buffer_Close[0];
       Buffer_Low[0] = Buffer_Close[0];
   }
   
   if (Buffer_Close[0] > Buffer_High[0])
   {
       Buffer_High[0] = Buffer_Close[0];
   }
   if (Buffer_Close[0] < Buffer_Low[0])
   {
       Buffer_Low[0] = Buffer_Close[0];
   }
//----
   return(0);
}


double GetRib(string symbol, int tf, int offset){
   int red_cnt = 0;
   int green_cnt = 0;
   double g_ima_188 = iMA(symbol, tf, 5, 0, MODE_EMA, PRICE_CLOSE, offset);
   double g_ima_196 = iMA(symbol, tf, 5, 0, MODE_EMA, PRICE_CLOSE, offset + 1);
   if (g_ima_188==0 || g_ima_196==0) return(100);
   if (g_ima_188 < g_ima_196) red_cnt++;
   else green_cnt++;
   g_ima_188 = iMA(symbol, tf, 10, 0, MODE_EMA, PRICE_CLOSE, offset);
   g_ima_196 = iMA(symbol, tf, 10, 0, MODE_EMA, PRICE_CLOSE, offset + 1);
   if (g_ima_188==0 || g_ima_196==0) return(100);
   if (g_ima_188 < g_ima_196) red_cnt++;
   else green_cnt++;
   g_ima_188 = iMA(symbol, tf, 20, 0, MODE_EMA, PRICE_CLOSE, offset);
   g_ima_196 = iMA(symbol, tf, 20, 0, MODE_EMA, PRICE_CLOSE, offset + 1);
   if (g_ima_188==0 || g_ima_196==0) return(100);
   if (g_ima_188 < g_ima_196) red_cnt++;
   else green_cnt++;
   g_ima_188 = iMA(symbol, tf, 30, 0, MODE_EMA, PRICE_CLOSE, offset);
   g_ima_196 = iMA(symbol, tf, 30, 0, MODE_EMA, PRICE_CLOSE, offset + 1);
   if (g_ima_188==0 || g_ima_196==0) return(100);
   if (g_ima_188 < g_ima_196) red_cnt++;
   else green_cnt++;
   if (red_cnt==4) return(-1);
   if (red_cnt==3) return(-0.5);
   if (green_cnt==4) return(1);
   if (green_cnt==3) return(0.5);
   return(0);
}

void LoadData(int tf, int offset){

   double v1;
   if (isEURUSD) EURUSD = GetRib("EURUSD",tf,offset);
   if (isEURGBP) EURGBP = GetRib("EURGBP",tf,offset);
   if (isEURCAD) EURCAD = GetRib("EURCAD",tf,offset);
   if (isEURCHF) EURCHF = GetRib("EURCHF",tf,offset);
   if (isEURJPY) EURJPY = GetRib("EURJPY",tf,offset);
   if (isEURAUD) EURAUD = GetRib("EURAUD",tf,offset);
   if (isEURNZD) EURNZD = GetRib("EURNZD",tf,offset);
   if (isGBPUSD) GBPUSD = GetRib("GBPUSD",tf,offset);
   if (isGBPCAD) GBPCAD = GetRib("GBPCAD",tf,offset);
   if (isGBPCHF) GBPCHF = GetRib("GBPCHF",tf,offset);
   if (isGBPJPY) GBPJPY = GetRib("GBPJPY",tf,offset);
   if (isGBPAUD) GBPAUD = GetRib("GBPAUD",tf,offset);
   if (isGBPNZD) GBPNZD = GetRib("GBPNZD",tf,offset);
   if (isUSDCAD) USDCAD = GetRib("USDCAD",tf,offset);
   if (isUSDCHF) USDCHF = GetRib("USDCHF",tf,offset);
   if (isUSDJPY) USDJPY = GetRib("USDJPY",tf,offset);
   if (isAUDUSD) AUDUSD = GetRib("AUDUSD",tf,offset);
   if (isNZDUSD) NZDUSD = GetRib("NZDUSD",tf,offset);
   if (isCADCHF) CADCHF = GetRib("CADCHF",tf,offset);
   if (isCADJPY) CADJPY = GetRib("CADJPY",tf,offset);
   if (isAUDCAD) AUDCAD = GetRib("AUDCAD",tf,offset);
   if (isNZDCAD) NZDCAD = GetRib("NZDCAD",tf,offset);
   if (isAUDCHF) AUDCHF = GetRib("AUDCHF",tf,offset);
   if (isAUDJPY) AUDJPY = GetRib("AUDJPY",tf,offset);
   if (isAUDNZD) AUDNZD = GetRib("AUDNZD",tf,offset);
   if (isNZDCHF) NZDCHF = GetRib("NZDCHF",tf,offset);
   if (isNZDJPY) NZDJPY = GetRib("NZDJPY",tf,offset);
   if (isCHFJPY) CHFJPY = GetRib("CHFJPY",tf,offset);
}

double GetStrength_Symbol ( string a_Symbol ) {
   double result = 0;
   if ( a_Symbol =="EUR" )  result = GetStrength_EUR();
   if ( a_Symbol =="GBP" )  result = GetStrength_GBP();
   if ( a_Symbol =="USD" )  result = GetStrength_USD();
   if ( a_Symbol =="CAD" )  result = GetStrength_CAD();
   if ( a_Symbol =="AUD" )  result = GetStrength_AUD();
   if ( a_Symbol =="NZD" )  result = GetStrength_NZD();
   if ( a_Symbol =="CHF" )  result = GetStrength_CHF();
   if ( a_Symbol =="JPY" )  result = GetStrength_JPY();
   return(result);
}



double GetStrength_EUR(){
   double result = 0;
   double v1;

   v1 = EURUSD;   if (v1>10) return(0);   result+=v1;
   v1 = EURGBP;   if (v1>10) return(0);   result+=v1;
   v1 = EURCAD;   if (v1>10) return(0);   result+=v1;
   v1 = EURCHF;   if (v1>10) return(0);   result+=v1;
   v1 = EURAUD;   if (v1>10) return(0);   result+=v1;
   v1 = EURNZD;   if (v1>10) return(0);   result+=v1;
   v1 = EURJPY;   if (v1>10) return(0);   result+=v1;
   
   return(result);
}

double GetStrength_GBP(){
   double result = 0;
   double v1;

   v1 = GBPUSD;   if (v1>10) return(0);   result+=v1;
   v1 = EURGBP;   if (v1>10) return(0);   result-=v1;
   v1 = GBPCAD;   if (v1>10) return(0);   result+=v1;
   v1 = GBPCHF;   if (v1>10) return(0);   result+=v1;
   v1 = GBPAUD;   if (v1>10) return(0);   result+=v1;
   v1 = GBPNZD;   if (v1>10) return(0);   result+=v1;
   v1 = GBPJPY;   if (v1>10) return(0);   result+=v1;
   
   return(result);
}

double GetStrength_USD(){
   double result = 0;
   double v1;

   v1 = EURUSD;   if (v1>10) return(0);   result-=v1;
   v1 = GBPUSD;   if (v1>10) return(0);   result-=v1;
   v1 = USDCAD;   if (v1>10) return(0);   result+=v1;
   v1 = USDCHF;   if (v1>10) return(0);   result+=v1;
   v1 = AUDUSD;   if (v1>10) return(0);   result-=v1;
   v1 = NZDUSD;   if (v1>10) return(0);   result-=v1;
   v1 = USDJPY;   if (v1>10) return(0);   result+=v1;
   
   return(result);
}

double GetStrength_CAD(){
   double result = 0;
   double v1;

   v1 = EURCAD;   if (v1>10) return(0);   result-=v1;
   v1 = GBPCAD;   if (v1>10) return(0);   result-=v1;
   v1 = USDCAD;   if (v1>10) return(0);   result-=v1;
   v1 = CADCHF;   if (v1>10) return(0);   result+=v1;
   v1 = AUDCAD;   if (v1>10) return(0);   result-=v1;
   v1 = NZDCAD;   if (v1>10) return(0);   result-=v1;
   v1 = CADJPY;   if (v1>10) return(0);   result+=v1;
   
   return(result);
}

double GetStrength_AUD(){
   double result = 0;
   double v1;

   v1 = AUDUSD;   if (v1>10) return(0);   result+=v1;
   v1 = GBPAUD;   if (v1>10) return(0);   result-=v1;
   v1 = AUDCAD;   if (v1>10) return(0);   result+=v1;
   v1 = AUDCHF;   if (v1>10) return(0);   result+=v1;
   v1 = EURAUD;   if (v1>10) return(0);   result-=v1;
   v1 = AUDNZD;   if (v1>10) return(0);   result+=v1;
   v1 = AUDJPY;   if (v1>10) return(0);   result+=v1;
   
   return(result);
}

double GetStrength_NZD(){
   double result = 0;
   double v1;

   v1 = NZDUSD;   if (v1>10) return(0);   result+=v1;
   v1 = GBPNZD;   if (v1>10) return(0);   result-=v1;
   v1 = NZDCAD;   if (v1>10) return(0);   result+=v1;
   v1 = NZDCHF;   if (v1>10) return(0);   result+=v1;
   v1 = EURNZD;   if (v1>10) return(0);   result-=v1;
   v1 = AUDNZD;   if (v1>10) return(0);   result-=v1;
   v1 = NZDJPY;   if (v1>10) return(0);   result+=v1;
   
   return(result);
}

double GetStrength_CHF(){
   double result = 0;
   double v1;

   v1 = USDCHF;   if (v1>10) return(0);   result-=v1;
   v1 = GBPCHF;   if (v1>10) return(0);   result-=v1;
   v1 = NZDCHF;   if (v1>10) return(0);   result-=v1;
   v1 = CADCHF;   if (v1>10) return(0);   result-=v1;
   v1 = EURCHF;   if (v1>10) return(0);   result-=v1;
   v1 = AUDCHF;   if (v1>10) return(0);   result-=v1;
   v1 = CHFJPY;   if (v1>10) return(0);   result+=v1;
   
   return(result);
}

double GetStrength_JPY(){
   double result = 0;
   double v1;

   v1 = USDJPY;   if (v1>10) return(0);   result-=v1;
   v1 = GBPJPY;   if (v1>10) return(0);   result-=v1;
   v1 = NZDJPY;   if (v1>10) return(0);   result-=v1;
   v1 = CADJPY;   if (v1>10) return(0);   result-=v1;
   v1 = EURJPY;   if (v1>10) return(0);   result-=v1;
   v1 = AUDJPY;   if (v1>10) return(0);   result-=v1;
   v1 = CHFJPY;   if (v1>10) return(0);   result-=v1;
   
   return(result);
}

void rm(string sym){
   string ID = Indicator_Name+sym;
   ObjectDelete(ID);
}

void DoAlerts(string as_sound, string as_8) {
      Alert(as_8);
      PlaySound(as_sound);
}

void creat_arr (string name, datetime arr_time, double arr_price , int arr_code, color a_color ) {
    ObjectDelete(name);
    ObjectCreate(name, OBJ_ARROW, 0,arr_time, arr_price);
    ObjectSet(name,OBJPROP_ARROWCODE,arr_code);
    ObjectSet(name,OBJPROP_COLOR,a_color );
}

bool isNewBar()
{
    if (lastTime == Time[0]) return (false);   
    lastTime = Time[0];
    return (true); 
}

